home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / MorphOS / Epic4_mos / share / epic / help / 5_programming / if < prev    next >
Encoding:
Text File  |  2002-10-28  |  3.8 KB  |  98 lines

  1. Synopsis:
  2.    if (<condition>) <then>
  3.    if (<condition>) { <then> } [{ <else> }]
  4.    if (<condition>) { <then> } elsif (<condition>) { <then> } else { <then> }
  5.  
  6. Description:
  7.    IF is the general purpose control statement for testing the truth/false
  8.    value of a given condition.  If the condition is true, it performs
  9.    some action; if false, some alternate action.  The condition does not
  10.    necessarily need to be a numeric comparison.  It may be a function whose
  11.    return value is evaluated for truth or falsity, or compared against some
  12.    other value (which might also be a function return value).  Expressions
  13.    are generally of the following forms:
  14.  
  15.       (  exp )           tests for existence of exp (usually a variable)
  16.       ( !exp )           tests for non-existence of exp
  17.       (  exp1 == exp2 )  tests whether exp1 equals exp2
  18.       (  exp1 != exp2 )  tests whether exp1 does not equal exp2
  19.       (  exp1 && exp2 )  tests for existence of exp1 and exp2
  20.       (  exp1 || exp2 )  tests for existence of exp1 or exp2 or both
  21.       (  exp1 ^^ exp2 )  tests for existence of exp1 or exp2, not both
  22.       (  exp1 <  exp2 )  tests whether exp1 is less than exp2
  23.       (  exp1 >  exp2 )  tests whether exp1 is more than exp2
  24.       (  exp1 <= exp2 )  tests whether exp1 is less than or equal to exp2
  25.       (  exp1 >= exp2 )  tests whether exp1 is more than or equal to exp2
  26.  
  27.    The "else" portion of an IF statement is not required.  Additionally,
  28.    if the "then" portion is only a single statement, the curly braces are
  29.    not required either.  The expression (exp) is evaluated as though it
  30.    were within a ${} construct, such that
  31.  
  32.       if ( blah ) ...
  33.  
  34.    would expand "blah" to $blah, then test the value of $blah.  Variables
  35.    can also be placed inside the expression parser, such that
  36.  
  37.       if ( [$blah] ) ...
  38.  
  39.    is equivalent to the previous statement (though it isn't as efficient).
  40.    Both forms may be combined in the same expression.  Numbers are treated
  41.    as constants, so in order to expand numeric expandos, such as $0, you
  42.    must use the expression parser, as above.  Strings must also be passed
  43.    through the expression parser (otherwise they are interpreted as
  44.    variables), and are compared case-insensitively.
  45.  
  46.    As in C, assignment operators may be used inside IF statements.  This is
  47.    generally not recommended, if only because it can make the code rather
  48.    confusing, but there are times when it can prove to be useful.  The
  49.    following:
  50.  
  51.       if ( foo = 3 > bar ) ...
  52.  
  53.    would first set the value of $foo to 3, and then compare it with $bar.
  54.    Note that the @ operator is not needed (and in fact is not even allowed).
  55.    Gratuitous use of parenthesis is recommended with this notation.
  56.  
  57.    Finally, as with other ircII-EPIC control statements, the curly braces
  58.    may be placed anywhere.
  59.  
  60. Examples:
  61.    The following two statements are functionally equivalent:
  62.       if ( foo == bar ) echo foo and bar are the same
  63.       if ( foo == bar ) {
  64.          echo foo and bar are the same
  65.       }
  66.  
  67.    These are also equivalent:
  68.       if ( !foo ) ...
  69.       unless ( foo ) ...
  70.  
  71.    Braces are required for a multi-line then portion:
  72.       if ( foo == bar ) {
  73.          echo foo and bar are the same
  74.          echo that's so cool!
  75.       }
  76.  
  77.    Like other control statements, IFs may be embedded:
  78.       if ( foo ) {
  79.          if ( bar ) {
  80.             echo foo and bar are the same
  81.             echo that's so cool!
  82.          }
  83.       }
  84.  
  85.    Function return values can be evaluated too:
  86.       if ( rmatch(foobar *blah* *bar) ) {
  87.          echo it matched
  88.       }
  89.  
  90. Aliases:
  91.    UNLESS is the exact opposite of IF.  It is essentially the same applying
  92.    the negation operator (!) to the entire IF condition.
  93.  
  94. Other Notes:
  95.    The "then" and "else" portions aren't required to contain anything.  Use
  96.    of the negation operator and UNLESS obsolete this practice, however.
  97.  
  98.